<?php
namespace Admin\Controller;
use Admin\Model\AreaModel;
use Admin\Model\Fast1Model;
use Common\Model\CommonModel;
use Think\Controller;

class Fast1Controller extends  BaseController{

    /**
     *
     */
    public function index(){
        // echo 1 ; exit;
        $fastModel=M('fast1');
        if(!empty($_GET['area_id'])){
            unset($_SESSION['like']['fast1']);
            $_SESSION['like']['fast1']=$_GET['area_id'];
            $condition1['name']=array('like',"%".$_GET['area_id']."%");
            $areas=M('area')->where($condition1)->select();
            $area_ids=array();
            foreach($areas as $key=>$value){
                $area_ids[]=$value['id'];
            }
            if(!empty($area_ids)){
                $condition['area_id']=array('in',$area_ids);
            }else{
                $this->display();
                exit;
            }
        }
        if($_GET['start']&&$_GET['end']){
            $condition['create']= array('between',array(strtotime($_GET['start']),strtotime($_GET['end'])));
        }

        $count = $fastModel->where($condition)->count();
        $page= new \Think\Page($count,1);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$fastModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();

        $list=CommonModel::status($list);
        $areaModel=new AreaModel();
        $list=$areaModel->getArea($list);
        //var_dump($list);exit;
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();

    }

    //修改页面
    public function update(){
        $fast1Model=D('fast1');
        //$fastModel=new PriceModel();
        $da['id']=$_GET['id'];
        //找到原数据
        $list=$fast1Model->finded($da);
        $areaModel=new AreaModel();
        $areas=$areaModel->getAll();
       // var_dump($list);exit;
        $this->assign('areas',$areas);
        $this->assign('list',$list);
        $this->display();
    }

    //执行修改
    public function doUpdate(){
        $fast1Model=D('fast1');
        $data['name']=$_POST['name'];
        $data['mobile']=$_POST['mobile'];
        $data['city']=$_POST['city'];
        $data['area_id']=$_POST['area_id'];
        $id=$_POST['id'];
        $where='id='.$id;
        //echo $id;exit;
        $list=$fast1Model->finded($where);
        //echo(json_encode($list)); exit;

        $data['update']=time();
        //过滤重复内容
        $data=CommonModel::filter($data,$list);
        // echo json_encode($data);
        // exit;
        $res=$fast1Model->update($where,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }
    }

    //删除
    public function del(){
        $fast1Model=D('fast1');
        $id=$_GET['id'];
        $res= $fast1Model->deleted('id='.$id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }






}